package cn.laixueit.controller;

import cn.laixueit.pojo.Teacher;
import cn.laixueit.service.TeacherService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;

@Controller
@RequestMapping("/teacher")
@Scope("prototype")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @RequestMapping("/save")
    public @ResponseBody boolean save(Teacher teacher){
        try {
            int count = teacherService.save(teacher);
            if(count>0){
                return true;
            }
            return false;
        }catch (Exception e) {
            return false;
        }
    }


    @RequestMapping("/findall")
    @ResponseBody
    public HashMap<String,Object> findall(@RequestParam(defaultValue = "1")Integer startpage,@RequestParam(defaultValue = "2")Integer pagesize,Teacher teacher){
        HashMap<String, Object> map = new HashMap<>();
        PageInfo<Teacher> pageInfo = teacherService.findall(startpage,pagesize,teacher);
        map.put("pageInfo",pageInfo);
        map.put("teacher",teacher);
        return map;
    }

    @GetMapping("/findbyid")
    public  @ResponseBody Teacher findbyid(Integer id){
        return teacherService.findbyid(id);
    }
}
